package com.admin.service;

import com.data.entity.Role;
import com.data.entity.RoleMenu;
import com.data.mapper.RoleMapper;
import com.data.mapper.RoleMenuMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @描述 <>
 * @参数 $params$
 * @返回值 $return$
 * @创建人 WangGuanghua
 * @创建时间 $date$
 */
@Service
public class RoleService {
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private RoleMenuMapper roleMenuMapper;

    public List<Role> list() {
        return  roleMapper.selectRoleList();
    }

    public long count() {
        return  roleMapper.selectRoleCount();
    }

    /**
     * 根据角色id查询角色与菜单关联信息
     * @return
     */
    public List<RoleMenu> relMenu(Long roleId){
        return roleMapper.roleMenus(roleId);
    }

    /**
     * 批量添加菜单角色关联信息
     * @param roleId
     */
    @Transactional
    public void addRoleMenu(Long roleId, List<Long> menuIds) {
        roleMapper.delRelMenuRoleByRoleId(roleId);
        for (int i=0;i<menuIds.size();i++){
            RoleMenu relRoleMenu=new RoleMenu();
            relRoleMenu.setMenuId(menuIds.get(i));
            relRoleMenu.setRoleId(roleId);
            roleMenuMapper.add(relRoleMenu);
        }
    }


    public Role getInfo(Long id) {
        return roleMapper.selectById(id);
    }

    public int edit(Role role) {
        return roleMapper.updateById(role);
    }

    public int add(Role role) {
        return roleMapper.insert(role);
    }

    public int delete(Long id) {
        return roleMapper.deleteById(id);
    }
}
